package com.dxl.web.account.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.bean.account.RoleNode;
import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.model.easyui.TreeNode;
import com.dxl.common.utils.Utils;
import com.dxl.entity.account.Role;
import com.dxl.service.account.RoleService;
import com.dxl.web.common.controller.BaseController;

/**
 * 角色信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/account/role")
@RequirePermission(value={"ROLE-MODULE"}, name="角色模块", desc="包括角色增、删、改、查及授权")
public class RoleController extends BaseController {
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("")
	@RequirePermission(value={"ROLE-VIEW"}, name="角色查看", desc="查看角色信息")
	public ModelAndView role() {
		ModelAndView mv = new ModelAndView("account/role/list");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/tree")
	@RequirePermission(value={"ROLE-VIEW"}, name="角色查看", desc="查看角色信息")
	public List<TreeNode> tree(String accountId) {
		return this.roleService.tree(accountId);
	}
	
	@ResponseBody
	@RequestMapping("/treegrid")
	@RequirePermission(value={"ROLE-VIEW"}, name="角色查看", desc="查看角色信息")
	public List<RoleNode> treegrid() {
		return this.roleService.treeGrid();
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"ROLE-VIEW"}, name="角色查看", desc="查看角色信息")
	public Page<Role> list(String parentId, String keyword, Integer state, Integer page, Integer rows) {
		Page<Role> p = this.roleService.list(parentId, keyword, state, page, rows);
		return p;
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"ROLE-VIEW"}, name="角色查看", desc="查看角色信息")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView("account/role/edit");
		if (Utils.isNotBlank(id)) {
			mv.addObject("entity", this.baseService.get(Role.class, id));
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"ROLE-MODIFY"}, name="角色修改", desc="修改角色信息")
	public RespData save(Role role) {
		return this.roleService.save(role);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"ROLE-DEL"}, name="角色删除", desc="删除角色信息")
	public RespData del(String[] ids) {
		return roleService.del(ids);
	}

	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"ROLE-STATE"}, name="角色状态修改", desc="修改角色状态")
	public RespData state(String[] ids, Integer state) {
		return roleService.state(ids, state);
	}
	
	@ResponseBody
	@RequestMapping(value = "/authorize", method = RequestMethod.GET)
	@RequirePermission(value={"ROLE-AUTHORIZE"}, name="角色授权", desc="角色授权")
	public ModelAndView authorize(String roleId) {
		ModelAndView mv = new ModelAndView("account/function/selection");
		if (Utils.isNotBlank(roleId)) {
			mv.addObject("role", this.baseService.get(Role.class, roleId));
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping(value = "/authorize", method = RequestMethod.POST)
	@RequirePermission(value={"ROLE-AUTHORIZE"}, name="角色授权", desc="角色授权")
	public RespData authorize(String roleId, String[] functionIds) {
		this.roleService.authorize(roleId, functionIds);
		return RespData.instance(RespData.SUCCESS);
	}
}
